Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint/ctypes: ext. abi fn-ptr in internal abi fn #108611

Conversation

davidtwco
Copy link
Member

@davidtwco davidtwco commented Mar 1, 2023

Fixes #94223.

  • In the improper ctypes lint, instead of skipping functions with internal ABIs, check that the signature doesn't contain any fn-ptr types with external ABIs that aren't FFI-safe.
  • When computing the ABI for fn-ptr types, remove an unwrap that assumed FFI-safe types in foreign fn-ptr types.
    • I'm not certain that this is the correct approach.

@rustbot
Copy link
Collaborator

rustbot commented Mar 1, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 1, 2023
compiler/rustc_lint/src/types.rs Outdated Show resolved Hide resolved
compiler/rustc_lint/src/types.rs Show resolved Hide resolved
@Noratrieb
Copy link
Member

This seems to be the wording that is already used, but I really don't like the internal vs external| wording here, it makes no sense. An extern "Rust" fnis not internal, it just has a Rust ABI. Maybec_likevsrust_like` would be a better name.

@davidtwco
Copy link
Member Author

This seems to be the wording that is already used, but I really don't like the internal vs external| wording here, it makes no sense. An extern "Rust" fnis not internal, it just has a Rust ABI. Maybec_likevsrust_like` would be a better name.

I agree, but I'll leave this for a follow-up.

@davidtwco davidtwco force-pushed the issue-94223-external-abi-fn-ptr-in-internal-abi-fn branch from eb578e5 to 5da1782 Compare March 2, 2023 12:44
Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused now.

Surely this should fail compilation:

#![deny(improper_ctypes_definitions, improper_ctypes)]
struct __(extern "C" fn([u8]));

And yet, it currently passes it ([play]). And, if I understood correctly, under this PR it would pass too...

Maybe instead of trying to deeply check types in extern items, we should actually just check all types, find extern ... fn(...) types and check their arguments?

@davidtwco davidtwco force-pushed the issue-94223-external-abi-fn-ptr-in-internal-abi-fn branch from 5da1782 to 3e7ca3e Compare March 6, 2023 14:08
@davidtwco
Copy link
Member Author

Maybe instead of trying to deeply check types in extern items, we should actually just check all types, find extern ... fn(...) types and check their arguments?

I've extended support to all types that could contain the extern fn-ptr types (which I think is the only thing that we need to check in otherwise okay types, i.e. fn not extern fn).

@WaffleLapkin
Copy link
Member

I've extended support to all types that could contain the extern fn-ptr types

I don't think that's necessarily enough.

For example this doesn't get linted, still:

#![deny(improper_ctypes_definitions, improper_ctypes)]
fn main() {
    let _: extern "C" fn([u8]) = todo!();
}

I would prefer using something like LateLintPass::check_ty, so that we are sure we lint every type that is written in the source... but check_ty gets only a hir::Ty so I'm not sure if it could work (I don't see an easy way to get rustc_middle::ty::Ty from it)

@davidtwco
Copy link
Member Author

davidtwco commented Mar 6, 2023

I would prefer using something like LateLintPass::check_ty, so that we are sure we lint every type that is written in the source... but check_ty gets only a hir::Ty so I'm not sure if it could work (I don't see an easy way to get rustc_middle::ty::Ty from it)

That's exactly why I haven't used check_ty, also because I didn't want to check anything in foreign items, as the ImproperCTypesDeclarations lint visitor handles those.

I think all I can do is implement check_expr? Not sure where I can get a hir::Ty for that though.. I could implement check_local to handle that case, but not sure if there others this would miss?

@WaffleLapkin
Copy link
Member

idk, I don't feel confident reviewing this PR, so I'll reroll
r? compiler

@rustbot rustbot assigned wesleywiser and unassigned WaffleLapkin Mar 7, 2023
@apiraino
Copy link
Contributor

I assume Wesley has not enough bandwidth right now, so I'll reroll

r? compiler

@rustbot rustbot assigned jackh726 and unassigned wesleywiser Apr 20, 2023
Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one thought, but r=me if you don't want to try to change things.

let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
let sig = self.cx.tcx.erase_late_bound_regions(sig);

let is_internal_abi = self.is_internal_abi(abi);
let check_ty = |this: &mut ImproperCTypesVisitor<'a, 'tcx>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this all seems like it should just be a part of a visitor with a flag for foreign abi or not.

@jackh726 jackh726 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2023
@davidtwco
Copy link
Member Author

@bors r=jackh726

I think that suggestion is best left for a follow-up.

@bors
Copy link
Contributor

bors commented Apr 24, 2023

📌 Commit 3e7ca3e has been approved by jackh726

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 24, 2023
@bors
Copy link
Contributor

bors commented Apr 25, 2023

⌛ Testing commit 3e7ca3e with merge f13b0551bf4f1aa76569c80852751b51c811f5fc...

@bors

This comment was marked as resolved.

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 25, 2023
@bors
Copy link
Contributor

bors commented Jun 29, 2023

📌 Commit f14e5778ca8dccef35f243cb8b7869fe239afb69 has been approved by jackh726

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 29, 2023
@bors
Copy link
Contributor

bors commented Jun 29, 2023

⌛ Testing commit f14e5778ca8dccef35f243cb8b7869fe239afb69 with merge be32e03c64ad0401ea676f7831ad574bc7740354...

@rust-log-analyzer

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 29, 2023
davidtwco added 6 commits July 3, 2023 13:40
Instead of skipping functions with internal ABIs, check that the
signature doesn't contain any fn-ptr types with external ABIs that
aren't FFI-safe.

Signed-off-by: David Wood <[email protected]>
Remove an `unwrap` that assumed FFI-safe types in foreign fn-ptr types.

Signed-off-by: David Wood <[email protected]>
Extend previous commit's support for checking for external fn-ptrs in
internal fn types to report errors for multiple found fn-ptrs.

Signed-off-by: David Wood <[email protected]>
Extend previous checks for external ABI fn-ptrs to use in internal
statics, constants, type aliases and algebraic data types.

Signed-off-by: David Wood <[email protected]>
This was causing compilation failures in the
performance benchmarking as diesel hit recursion
limits.

Signed-off-by: David Wood <[email protected]>
@davidtwco davidtwco force-pushed the issue-94223-external-abi-fn-ptr-in-internal-abi-fn branch from f14e577 to 2227422 Compare July 3, 2023 13:29
@davidtwco
Copy link
Member Author

@bors r=jackh726

@bors
Copy link
Contributor

bors commented Jul 3, 2023

📌 Commit 2227422 has been approved by jackh726

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 3, 2023
@bors
Copy link
Contributor

bors commented Jul 3, 2023

⌛ Testing commit 2227422 with merge 0ab38e9...

@bors
Copy link
Contributor

bors commented Jul 3, 2023

☀️ Test successful - checks-actions
Approved by: jackh726
Pushing 0ab38e9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 3, 2023
@bors bors merged commit 0ab38e9 into rust-lang:master Jul 3, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jul 3, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0ab38e9): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
2.6% [2.5%, 2.7%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.3% [-1.7%, -0.9%] 2
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 670.457s -> 669.062s (-0.21%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet